package com.ruoyi.delivery.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.delivery.domain.DelDriver;
import com.ruoyi.delivery.service.IDelDriverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/delivery/driver")
public class DelDriverController {

    @Autowired
    private IDelDriverService driverService;

    /**
     * 获取司机信息列表，支持分页和筛选
     *
     * @param driverNo    司机编号
     * @param driverCity  当前城市
     * @param driverPhone 联系方式
     * @param vehicleNo    驾驶车辆编号
     * @param startDate   入职时间开始范围
     * @param endDate     入职时间结束范围
     * @param driverState 状态
     * @param page        页码
     * @param pageSize    每页记录数
     * @return 分页司机信息列表
     */
    @GetMapping("/list")
    public Map<String, Object> getDriverList(
            @RequestParam(required = false) String driverNo,
            @RequestParam(required = false) String driverCity,
            @RequestParam(required = false) String driverPhone,
            @RequestParam(required = false) String vehicleNo,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(required = false) Integer driverState,
            @RequestParam(required = false) String driverName,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize
    ) {
        String logisticsDriver = String.valueOf(SecurityUtils.getUserId());
        int offset = (page - 1) * pageSize;
        List<DelDriver> drivers = driverService.getDriverList(logisticsDriver, driverNo, driverCity, driverPhone, vehicleNo, startDate, endDate, driverState,driverName, offset, pageSize);
        int total = driverService.getDriverCount(logisticsDriver, driverNo, driverCity, driverPhone, vehicleNo, startDate, endDate, driverState, driverName);

        Map<String, Object> response = new HashMap<>();
        response.put("data", drivers);
        response.put("total", total);
        response.put("page", page);
        response.put("pageSize", pageSize);
        return response;
    }

    /**
     * 新建司机信息
     *
     * @param driver 司机信息对象
     * @return 操作成功消息
     */
    @PostMapping
    public Map<String, String> insertDriverInfo(@RequestBody DelDriver driver) {
        String logisticsDeliver = String.valueOf(SecurityUtils.getUserId());
        driverService.insertDriverInfo(driver);
        Map<String, String> response = new HashMap<>();
        response.put("message", "新建司机信息成功");
        return response;
    }

    /**
     * 更新司机信息
     *
     * @param driver 司机信息对象
     * @return 操作成功消息
     */
    @PutMapping
    public Map<String, String> updateDriverInfo(@RequestBody DelDriver driver) {
        driverService.updateDriverInfo(driver);
        Map<String, String> response = new HashMap<>();
        response.put("message", "更新司机信息成功");
        return response;
    }

    /**
     * 删除司机信息
     *
     * @param driverNo 司机编号
     * @return 操作成功消息
     */
    @DeleteMapping("/delete/{driverNo}")
    public Map<String, String> deleteDriver(@PathVariable String driverNo) {
        driverService.deleteDriver(driverNo);
        Map<String, String> response = new HashMap<>();
        response.put("message", "删除司机信息成功");
        return response;
    }

    /**
     * 批量删除司机信息
     *
     * @param driverNos 司机编号列表
     * @return 操作成功消息
     */
    @PostMapping("/batchDelete")
    public Map<String, String> deleteDrivers(@RequestBody List<String> driverNos) {
        driverService.deleteDrivers(driverNos);
        Map<String, String> response = new HashMap<>();
        response.put("message", "批量删除司机信息成功");
        return response;
    }
}
